<html>
<head>
<title>firstworks   Programming with SQL Relay using the Ruby DBI API</title>
<link href="../css/styles.css" rel="stylesheet">
</head>
<body>

<span class="heading1">Programming with SQL Relay using the Ruby DBI API</span><br><br>

<ul>
<li><a href="#session">Establishing a Sessions</a></li>
<li><a href="#query">Executing Queries</a></li>
<li><a href="#commit">Commits and Rollbacks</a></li>
<li><a href="#temptables">Temporary Tables</a></li>
<li><a href="#errors">Catching Errors</a></li>
<li><a href="#bindvars">Bind Variables</a></li>
<li><a href="#rebinding">Re-Binding and Re-Executing</a></li>
<li><a href="#fields">Accessing Fields in the Result Set</a></li>
<li><a href="#cursors">Concurrent Statements</a></li>
<li><a href="#columns">Getting Column Information</a></li>
<li><a href="#storedprocedures">Stored Procedures</a></li>
<li><a href="#lastinsertid">Getting the Last Insert ID in SQLite</a></li>
</ul>

<a name="session"></a>
<span class="heading2">Establishing a Session</span><br><br>

<p>To use SQL Relay, you have to identify the connection that you intend to 
use.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

... execute some queries ...
</PRE>
</blockquote>

<p>After calling connect() and prepare(), a session is established when the
first execute() is run.</p>

<p>For the duration of the session, the client stays connected to a database
connection daemon.  While one client is connected, no other client can connect.
Care should be taken to minimize the length of a session.</p>

<p>If you're using a transactional database, ending a session has a catch.
Database connection daemons can be configured to send either a commit or
rollback at the end of a session if DML queries were executed during the
session with no commit or rollback.  Program accordingly.</p>

<a name="query"></a>
<span class="heading2">Executing Queries</span><br><br>

<p>Call prepare() and execute() to run a query.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt.execute()

... process the result set ...
</PRE>
</blockquote>

<a name="commit"></a>
<span class="heading2">Commits and Rollbacks</span><br><br>

<p>If you need to execute a commit or rollback, you should use the commit()
and rollback() methods rather than sending a "commit" or "rollback" query.  
There are two reasons for this.  First, it's much more efficient to call the 
methods.  Second, if you're writing code that can run on transactional or 
non-transactional databases, some non-transactional databases will throw errors
if they receive a "commit" or "rollback" query, but by calling the commit() 
and rollback() methods you instruct the database connection daemon to call the 
commit and rollback API methods for that database rather than issuing them as 
queries.  If the API's have no commit or rollback methods, the calls do nothing
and the database throws no error.  This is especially important when using SQL 
Relay with ODBC.</p>

<p>You can also turn Autocommit on or off by setting the AutoCommit attribute
of the database handle.</p>

<p>The following command turns Autocommit on.</p>

<blockquote>
<PRE>
db[<FONT color=#6959cf>&quot;</FONT><FONT color=#ff00ff>AutoCommit</FONT><FONT color=#6959cf>&quot;</FONT>]=<FONT color=#ff00ff>true</FONT>
</PRE>
</blockquote>

<p>The following command turns Autocommit off.</p>

<blockquote>
<PRE>
db[<FONT color=#6959cf>&quot;</FONT><FONT color=#ff00ff>AutoCommit</FONT><FONT color=#6959cf>&quot;</FONT>]=<FONT color=#ff00ff>false</FONT>
</PRE>
</blockquote>

<p>When Autocommit is on, the database performs a commit after each successful 
DML or DDL query.  When Autocommit is off, the database commits when the client 
instructs it to, or (by default) when a client disconnects.  For databases that
don't support Autocommit, setting the AutoCommit attribute has no effect.</p>

<a name="temptables"></a>
<span class="heading2">Temporary Tables</span><br><br>

<p>Some databases support temporary tables.  That is, tables which are
automatically dropped or truncated when an application closes its connection
to the database or when a transaction is committed or rolled back.</p>

<p>For databases which drop or truncate tables when a transaction is committed
or rolled back, temporary tables work naturally.</p>

<p>However, for databases which drop or truncate tables when an application
closes its connection to the database, there is an issue.  Since SQL Relay
maintains persistent database connections, when an application disconnects from
SQL Relay, the connection between SQL Relay and the database remains, so
the database does not know to drop or truncate the table.  To remedy this
situation, SQL Relay parses each query to see if it created a temporary table,
keeps a list of temporary tables and drops (or truncates them) when the
application disconnects from SQL Relay.  Since each database has slightly
different syntax for creating a temporary table, SQL Relay parses each query
according to the rules for that database.</p>

<p>In effect, temporary tables should work when an application connects to
SQL Relay in the same manner that they would work if the application connected
directly to the database.</p>

<a name="errors"></a>
<span class="heading2">Catching Errors</span><br><br>

<p>If your call to execute() raises an exception, the query failed.
You can find out why by catching the exception.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

<B><FONT color=#a52a2a>begin</FONT></B>
        db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

        stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable</FONT><FONT color=#6a5acd>&quot;</FONT>)

        stmt.execute()

<B><FONT color=#a52a2a>rescue</FONT></B> <FONT color=#6a5acd>DBI</FONT>::<FONT color=#6a5acd>ProgrammingError</FONT> =&gt; error
        print error
        print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
</PRE>
</blockquote>

<a name="bindvars"></a>
<span class="heading2">Bind Variables</span><br><br>

<p>Programs rarely execute fixed queries.  More often than not, some part
of the query is dynamically generated.  The Ruby DBI API provides the 
bind_param method for using bind variables in those queries.</p>

<p>For a detailed discussion of binds, see 
<a href="binds.html">this document</a>.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable where column1&gt;:val1 and column2=:val2 and column3&lt;val3</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val1</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>1</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val2</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>hello</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val3</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>50.546</FONT>,<FONT color=#ff00ff>false</FONT>)

stmt.execute()

... process the result set ...

</PRE>
</blockquote>

<p>When passing a floating point number in as a bind or substitution variable,
you have to supply precision and scale for the number.  See
<a href="precisionscale.html">this page</a> for a discussion of precision and
scale.</p>

<a name="rebinding"></a>
<span class="heading2">Re-Binding and Re-Execution</span><br><br>

<p>A feature of the prepare/bind/execute paradigm is the ability to prepare, 
bind and execute a query once, then re-bind and re-execute the query 
over and over without re-preparing it.  If your backend database natively 
supports this paradigm, you can reap a substantial performance improvement.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable where column1&amp;gt;:val1 and column2=:val2 and column3&amp;lt;val3</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val1</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>1</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val2</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>hello</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val3</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>1.1</FONT>,<FONT color=#ff00ff>false</FONT>)

stmt.execute()

... process the result set ...

stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val1</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>2</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val2</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>hi</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val3</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>2.22</FONT>,<FONT color=#ff00ff>false</FONT>)

stmt.execute()

... process the result set ...

stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val1</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>3</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val2</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>bye</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>false</FONT>)
stmt.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>val3</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#ff00ff>3.333</FONT>,<FONT color=#ff00ff>false</FONT>)

stmt.execute()

... process the result set ...

</PRE>
</blockquote>

<a name="fields"></a>
<span class="heading2">Accessing Fields in the Result Set</span><br><br>

<p>The fetch(), fetch_many() and fetch_all() methods are useful for processing
result sets.  fetch() returns a list of values.  fetch_many() and fetch_all()
each return an Array of rows where each row is an Array of values.</p>

<p>The rows() method gives the number of rows in the result set of a select 
query.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt.execute()

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>rowcount: </FONT><FONT color=#6a5acd>&quot;</FONT>+stmt.rows().to_s+<FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the first row:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch()
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the next three rows:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_many()
        <B><FONT color=#a52a2a>for</FONT></B> j <B><FONT color=#a52a2a>in</FONT></B> i
                print j+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
        <B><FONT color=#a52a2a>end</FONT></B>
        print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the rest of the rows:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_all()
        <B><FONT color=#a52a2a>for</FONT></B> j <B><FONT color=#a52a2a>in</FONT></B> i
                print j+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
        <B><FONT color=#a52a2a>end</FONT></B>
        print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>

</PRE>
</blockquote>

<p>The fetch_scroll() method provides arbitrary access to the result set.  You
can use it to skip forward or backward.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt.execute()

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the first row:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_scroll(<FONT color=#6a5acd>DBD</FONT>:<FONT color=#6a5acd>SQL_FETCH_FIRST</FONT>)
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the last row:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_scroll(<FONT color=#6a5acd>DBD</FONT>:<FONT color=#6a5acd>SQL_FETCH_LAST</FONT>)
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the second to last row:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_scroll(<FONT color=#6a5acd>DBD</FONT>:<FONT color=#6a5acd>SQL_FETCH_PRIOR</FONT>)
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the last row again:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_scroll(<FONT color=#6a5acd>DBD</FONT>:<FONT color=#6a5acd>SQL_FETCH_NEXT</FONT>)
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the first row again:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_scroll(<FONT color=#6a5acd>DBD</FONT>:<FONT color=#6a5acd>SQL_FETCH_ABSOLUTE</FONT>,<FONT color=#ff00ff>0</FONT>)
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>the 4th row:</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.fetch_scroll(<FONT color=#6a5acd>DBD</FONT>:<FONT color=#6a5acd>SQL_FETCH_RELATIVE</FONT>,<FONT color=#ff00ff>3</FONT>)
        print i+<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>,</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
print <FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n\n</FONT><FONT color=#6a5acd>&quot;</FONT>

</PRE>
</blockquote>

<a name="cursors"></a>
<span class="heading2">Concurrent Statements</span><br><br>

<p>It is possible to execute queries while processing the result set of another
query.  You can select rows from a table in one query, then iterate through 
its result set, inserting rows into another table, using only 1 database 
connection for both operations.</p>

<p>For example:</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt1=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable</FONT><FONT color=#6a5acd>&quot;</FONT>)
stmt2=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>insert into my_other_table values (:var1,:var2:,var3)</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt1.execute()

<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt1.fetch_all():
        stmt2.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>var1</FONT><FONT color=#6a5acd>&quot;</FONT>,i[<FONT color=#ff00ff>0</FONT>],<FONT color=#ff00ff>false</FONT>)
        stmt2.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>var2</FONT><FONT color=#6a5acd>&quot;</FONT>,i[<FONT color=#ff00ff>1</FONT>],<FONT color=#ff00ff>false</FONT>)
        stmt2.bind_param(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>var3</FONT><FONT color=#6a5acd>&quot;</FONT>,i[<FONT color=#ff00ff>2</FONT>],<FONT color=#ff00ff>false</FONT>)
        stmt2.execute()
<B><FONT color=#a52a2a>end</FONT></B>
</PRE>
</blockquote>

<a name="columns"></a>
<span class="heading2">Getting Column Information</span><br><br>

<p>After executing a query, column information can be retrieved using the
column_info() method.  column_info() returns an Array of hashes.  Each hash
contains 'name', 'type_name' and 'precision' keys.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>require</FONT> <FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>dbi</FONT><FONT color=#6a5acd>'</FONT>

db=<FONT color=#6a5acd>DBI</FONT>.connect(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>DBI:SQLRelay:host=localhost;port=9000;socket=/tmp/mysock.socket</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>myuser</FONT><FONT color=#6a5acd>&quot;</FONT>,<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>mypassword</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt=db.prepare(<FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>select * from mytable</FONT><FONT color=#6a5acd>&quot;</FONT>)

stmt.execute()

<B><FONT color=#a52a2a>for</FONT></B> i <B><FONT color=#a52a2a>in</FONT></B> stmt.column_info()
        print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>Name:       </FONT><FONT color=#6a5acd>&quot;</FONT>+i[<FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>name</FONT><FONT color=#6a5acd>'</FONT>]+<FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
        print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>Type:       </FONT><FONT color=#6a5acd>&quot;</FONT>+i[<FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>type_name</FONT><FONT color=#6a5acd>'</FONT>]+<FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
        print <FONT color=#6a5acd>&quot;</FONT><FONT color=#ff00ff>Length:     </FONT><FONT color=#6a5acd>&quot;</FONT>+i[<FONT color=#6a5acd>'</FONT><FONT color=#ff00ff>precision</FONT><FONT color=#6a5acd>'</FONT>]+<FONT color=#6a5acd>&quot;</FONT><FONT color=#6a5acd>\n</FONT><FONT color=#6a5acd>&quot;</FONT>
<B><FONT color=#a52a2a>end</FONT></B>
</PRE>
</blockquote>

<a name="storedprocedures"></a>
<span class="heading2">Stored Procedures</span><br><br>

<p>Many databases support stored procedures.  Stored procedures are sets of
queries and procedural code that are executed inside of the database itself.
For example, a stored procedure may select rows from one table, iterate through
the result set and, based on the values in each row, insert, update or delete
rows in other tables.  A client program could do this as well, but a stored
procedure is generally more efficient because queries and result sets don't
have to be sent back and forth between the client and database.  Also, stored
procedures are generally stored in the database in a compiled state, while
queries may have to be re-parsed and re-compiled each time they are sent.</p>

<p>While many databases support stored procedures.  The syntax for creating
and executing stored procedures varies greatly between databases.</p>

<p>SQL Relay supports stored procedures for most databases, but there are
some caveats.  Stored procedures are not currently supported when using FreeTDS
against Sybase or Microsoft SQL Server.  Blob/Clob bind variables are only
supported in Oracle 8i or higher.  Sybase stored procedures must use varchar
output parameters.</p>

<p>Stored procedures typically take input paramters from client programs through
input bind variables and return values back to client programs either through
bind variables or result sets.  Stored procedures can be broken down into
several categories, based on the values that they return.  Some stored
procedures don't return any values, some return a single value, some return
multiple values and some return entire result sets.</p>

<span class="heading3">No Values</span><br>

<p>Some stored procedures don't return any values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>) <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">begin testproc(:in1,:in2,:in3); end;</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc @in1 int, @in2 <font color="#298a52"><b>float</b></font>, @in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>) <font color="#6b59ce">as</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (@in1,@in2,@in3)
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">exec testproc</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">exec procedure testproc ?,?,?</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">DB2</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(<font color="#a52829"><b>in</b></font> in1 int, <font color="#a52829"><b>in</b></font> in2 double, <font color="#a52829"><b>in</b></font> in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) language sql
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">call testproc(?,?,?)</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns void <font color="#6b59ce">as</font> '
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> ($<font color="#ff00ff">1</font>,$<font color="#ff00ff">2</font>,$<font color="#ff00ff">3</font>);
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select testfunc(:in1,:in2,:in3)</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
<font color="#6a5acd">begin</font>
        <font color="#a52a2a"><b>insert</b></font> <font color="#6a5acd">into</font> mytable <font color="#6a5acd">values</font> (in1,in2,in3);
<font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">call testfunc(:in1,:in2,:in3)</font><font color="#6a5acd">&quot;</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in1</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in2</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in3</font><font color="#6a5acd">&quot;</font>,<font color="#6a5acd">&quot;</font><font color="#ff00ff">hello</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>


<br><span class="heading3">Single Values</span><br>

<p>Some stored procedures return single values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>) returns <font color="#298a52"><b>number</b></font> <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        <font color="#6b59ce">return</font> in1;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select testproc(:in1,:in2,:in3) from dual</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
result=stmt.fetch()[<font color="#ff00ff">0</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testproc
</pre>
</blockquote>

<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.  However, the SQL Relay Python DBdriver does not currently support
output parameters.</p>


<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns (out1 <font color="#298a52"><b>integer</b></font>) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1=in1;
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select * from testproc(:in1,:in2,:in3)</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
result=stmt.fetch()[<font color="#ff00ff">0</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">DB2</span><br>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.  However, the SQL Relay Python
DB driver does not currently support output parameters.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>)) returns int <font color="#6b59ce">as</font> '
declare
        in1 int;
        in2 <font color="#298a52"><b>float</b></font>;
        in3 <font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>);
<font color="#6b59ce">begin</font>
        in1:=$<font color="#ff00ff">1</font>;
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select * from testfunc(:in1,:in2,:in3)</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
result=stmt.fetch()[<font color="#ff00ff">0</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>A single value can be returned from a MySQL function.</p>

<p>To create the function, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">function</font> testfunc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns int <font color="#6a5acd">return</font> in1;
</pre>
</blockquote>

<p>To execute the function from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">select testfunc(?,?,?)</font><font color="#6a5acd">&quot;</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">1</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">2</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">3</font><font color="#6a5acd">&quot;</font>,<font color="#6a5acd">&quot;</font><font color="#ff00ff">hello</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
result=stmt.fetch()[<font color="#ff00ff">0</font>]
</pre>
</blockquote>

<p>To drop the function, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">function</font> testfunc
</pre>
</blockquote>

<p>A single value can be returned in the result set of a MySQL procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc() <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font>; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">select testfunc()</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
result=stmt.fetch()[<font color="#ff00ff">0</font>]
</pre>
</blockquote>

<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<p>A single value can be returned using the output variable of a MySQL
procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>out</b></font> out1 int) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font> <font color="#6a5acd">into</font> out1; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">set @out1=0</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">call testproc(@out1)</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">select @out1</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
result=stmt.fetch()[<font color="#ff00ff">0</font>]
</pre>
</blockquote>

<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>





<br><span class="heading3">Multiple Values</span><br>

<p>Some stored procedures return multiple values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.  If a procedure needs to return
multiple values, it can return one of them as the return value of the procedure
itself, but the rest must be returned through output parameters.  However, the
SQL Relay Python DB driver does not currently support output parameters.</p>

<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.  However, the SQL Relay Python DB driver does not currently support
output parameters.</p>

<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns (out1 <font color="#298a52"><b>integer</b></font>, out2 <font color="#298a52"><b>float</b></font>, out3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1=in1;
        out2=in2;
        out3=in3;
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select * from testfunc(?,?,?)</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
out1=stmt.fetch()[<font color="#ff00ff">0</font>]
out2=stmt.fetch()[<font color="#ff00ff">1</font>]
out3=stmt.fetch()[<font color="#ff00ff">2</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">DB2</span><br>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.  However, the SQL Relay Python
DB driver does not currently support output parameters.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>)) returns record <font color="#6b59ce">as</font> '
declare
        output record;
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>select</b></font> $<font color="#ff00ff">1</font>,$<font color="#ff00ff">2</font>,$<font color="#ff00ff">3</font> <font color="#6b59ce">into</font> output;
        <font color="#6b59ce">return</font> output;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
out1=stmt.fetch()[<font color="#ff00ff">0</font>]
out2=stmt.fetch()[<font color="#ff00ff">1</font>]
out3=stmt.fetch()[<font color="#ff00ff">2</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>Here's how you can get multiple values from the result
set of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> in1, in2, in3; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">select testfunc(:in1,:in2,:in3)</font><font color="#6a5acd">&quot;</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in1</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in2</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in3</font><font color="#6a5acd">&quot;</font>,<font color="#6a5acd">&quot;</font><font color="#ff00ff">hello</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
out1=stmt.fetch()[<font color="#ff00ff">0</font>]
out2=stmt.fetch()[<font color="#ff00ff">1</font>]
out3=stmt.fetch()[<font color="#ff00ff">2</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<p>Here's how you can get multiple values from the output variables
of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>out</b></font> out1 int, <font color="#a52a2a"><b>out</b></font> out2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>out</b></font> out3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">'hello'</font> <font color="#6a5acd">into</font> out1, out2, out3; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">set @out1=0,@out2=0.0,@out3=''</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">call testfunc(@out1,@out2,@out3)</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">select @out1,@out2,@out3)</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
out1=stmt.fetch()[<font color="#ff00ff">0</font>]
out2=stmt.fetch()[<font color="#ff00ff">1</font>]
out3=stmt.fetch()[<font color="#ff00ff">2</font>]
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>



<br><span class="heading3">Result Sets</span><br>

<p>Some stored procedures return entire result sets.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>Stored procedures in Oracle can return open cursors as return values or
output parameters.  A client-side cursor can be bound to this open cursor and
rows can be fetched from it.  However, the SQL Relay Python DB driver does not
currently support output parameters.</p>

<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc <font color="#6a5acd">as</font> <font color="#a52a2a"><b>select</b></font> * <font color="#6a5acd">from</font> testtable
</pre>
</blockquote>

<p>To exceute the stored procedure from an SQL Relay program, ue code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">exec testproc</font><font color="#6a5acd">&quot;</font>)
stmt.execute()
row1=stmt.fetch()
row2=stmt.fetch()
row3=stmt.fetch()
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Firebird</span><br>

<p>Stored procedures in Firebird can return a result set if a
select query in the procedure selects values into the output parameters and
then issues a suspend command, however SQL Relay doesn't currently support
stored procedures that return result sets.</p>

<span class="heading4">DB2</span><br>

<p>Stored procedures in DB2 can return a result set if the procedure is declared
to return one, however SQL Relay doesn't currently support stored procedures
that return result sets.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc() returns setof record <font color="#6b59ce">as</font> '
        declare output record;
<font color="#6b59ce">begin</font>
        <font color="#6b59ce">for</font> output <font color="#a52829"><b>in</b></font> <font color="#a52829"><b>select</b></font> * <font color="#6b59ce">from</font> mytable <font color="#6b59ce">loop</font>
                <font color="#6b59ce">return</font> next output;
        <font color="#6b59ce">end</font> <font color="#6b59ce">loop</font>;
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6b59ce">&quot;</font><font color="#ff00ff">select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))</font><font color="#6b59ce">&quot;</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in1</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in2</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6b59ce">&quot;</font><font color="#ff00ff">in3</font><font color="#6b59ce">&quot;</font>,<font color="#6b59ce">&quot;</font><font color="#ff00ff">hello</font><font color="#6b59ce">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
row1=stmt.fetch()
row2=stmt.fetch()
row3=stmt.fetch()
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>The result sets of all select statements called within MySQL stored
procedures (that aren't selected into variables) are returned from the procedure
call.  Though MySQL stored procedures can return multiple result sets,
currently SQL Relay can only fetch the first result set.</p>

<p>To create the stored procedure which returns a result set, run a query like
the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc() <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> * <font color="#6a5acd">from</font> mytable; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
stmt=db.prepare(<font color="#6a5acd">&quot;</font><font color="#ff00ff">call testfunc(:in1,:in2,:in3)</font><font color="#6a5acd">&quot;</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in1</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in2</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">false</font>)
stmt.bind_param(<font color="#6a5acd">&quot;</font><font color="#ff00ff">in3</font><font color="#6a5acd">&quot;</font>,<font color="#6a5acd">&quot;</font><font color="#ff00ff">hello</font><font color="#6a5acd">&quot;</font>,<font color="#ff00ff">false</font>)
stmt.execute()
row1=stmt.fetch()
row2=stmt.fetch()
row3=stmt.fetch()
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<a name="lastinsertid"></a>
<span class="heading2">Getting the Last Insert ID in SQLite</span><br><br>

<p>The SQLite API provides a function which returns the "last insert rowid";
the rowid of the most recent insert into the database or 0 if no inserts have 
ever been performed during the session.</p>

<p>SQL Relay supports fetching the last insert rowid from an SQLite database
as well.  You just have to run the query:

<blockquote>
<b>select last insert rowid</b>
</blockquote>


</body>
</html>
